<html>
<head><meta charset="utf-8"><title>Applications of trait objects as a data members · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Applications.20of.20trait.20objects.20as.20a.20data.20members.html">Applications of trait objects as a data members</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="232444830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Applications%20of%20trait%20objects%20as%20a%20data%20members/near/232444830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Mazakas <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Applications.20of.20trait.20objects.20as.20a.20data.20members.html#232444830">(Mar 30 2021 at 15:24)</a>:</h4>
<p>Hey everyone, I was wondering something...</p>
<p>Right now, Rust lets you write something such as:</p>
<div class="codehilite"><pre><span></span><code>trait X {}

struct Tester {
    x: dyn X,
}
</code></pre></div>
<p>and this makes <code>Tester</code> a dynamically-sized type.</p>
<p>My question is, are there any practical applications for a pattern like this?</p>
<p>Why does Rust allow us to treat trait objects as values even though they're impossible to use in most scenarios?</p>



<a name="232446935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Applications%20of%20trait%20objects%20as%20a%20data%20members/near/232446935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Applications.20of.20trait.20objects.20as.20a.20data.20members.html#232446935">(Mar 30 2021 at 15:37)</a>:</h4>
<p>This usually makes sense if <code>Tester</code> was generic, that way you can downcast a reference to it to <code>&amp;Tester&lt;dyn Trait&gt;</code></p>



<a name="232449918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Applications%20of%20trait%20objects%20as%20a%20data%20members/near/232449918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Applications.20of.20trait.20objects.20as.20a.20data.20members.html#232449918">(Mar 30 2021 at 15:56)</a>:</h4>
<p>Better seen with an example (also note that your question also applies to slices <em>vs.</em> arrays):</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">X</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* … */</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Tester</span><span class="o">&lt;</span><span class="n">T</span><span class="w"> </span>: <span class="o">?</span><span class="nb">Sized</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">X</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">dyn</span><span class="w"> </span><span class="n">X</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span>: <span class="nc">T</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* … */</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">X</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* … */</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="kd">let</span><span class="w"> </span><span class="n">tester</span>: <span class="kp">&amp;</span><span class="nc">Tester</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Tester</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* … */</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="c1">// above line is actually:</span>
<span class="kd">let</span><span class="w"> </span><span class="n">tester</span>: <span class="kp">&amp;</span><span class="nc">Tester</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="n">Tester</span>::<span class="o">&lt;</span><span class="n">Foo</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* … */</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// : &amp;Tester&lt;Foo&gt;</span>
<span class="w">    </span><span class="k">as</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Tester</span><span class="w"> </span><span class="cm">/* &lt;dyn X &gt; */</span><span class="w"></span>
<span class="p">;</span><span class="w"></span>
</code></pre></div>



<a name="232459009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Applications%20of%20trait%20objects%20as%20a%20data%20members/near/232459009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Mazakas <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Applications.20of.20trait.20objects.20as.20a.20data.20members.html#232459009">(Mar 30 2021 at 16:52)</a>:</h4>
<p>Hmm, that looks like you're still just storing a concrete instance though. Can you implement this without it being a generic param?</p>



<a name="232469058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Applications%20of%20trait%20objects%20as%20a%20data%20members/near/232469058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Applications.20of.20trait.20objects.20as.20a.20data.20members.html#232469058">(Mar 30 2021 at 17:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="399737">Christian Mazakas</span> <a href="#narrow/stream/122651-general/topic/Applications.20of.20trait.20objects.20as.20a.20data.20members/near/232444830">said</a>:</p>
<blockquote>
<p>Why does Rust allow us to treat trait objects as values even though they're impossible to use in most scenarios?</p>
</blockquote>
<p>They're like <code>str</code> -- you can use them, but usually behind indirection.  So you'll have <code>x: Box&lt;dyn X&gt;</code>, like you can have <code>Box&lt;str&gt;</code> or <code>&amp;str</code> or ...</p>



<a name="232475329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Applications%20of%20trait%20objects%20as%20a%20data%20members/near/232475329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Applications.20of.20trait.20objects.20as.20a.20data.20members.html#232475329">(Mar 30 2021 at 18:37)</a>:</h4>
<p><span class="user-mention silent" data-user-id="399737">Christian Mazakas</span> <a href="#narrow/stream/122651-general/topic/Applications.20of.20trait.20objects.20as.20a.20data.20members/near/232459009">said</a>:</p>
<blockquote>
<p>Hmm, that looks like you're still just storing a concrete instance though.</p>
</blockquote>
<p>You start by storing a concrete instance somewhere, and then coerce it to get the desired type-erasure (in my examples, I was having a a <code>&amp;Tester&lt;dyn X&gt;</code>), thanks to the (implicit on the first line, explicit on the second line) coercion:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">X</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* … */</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">testers</span>: <span class="nb">Vec</span><span class="o">&lt;&amp;'</span><span class="nb">_</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">Tester</span><span class="w"> </span><span class="cm">/* &lt;dyn X&gt; */</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[];</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">storage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Tester</span><span class="w"> </span><span class="cm">/* ::&lt;Foo&gt; */</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* … */</span><span class="w"> </span><span class="p">}};</span><span class="w"></span>
<span class="n">testers</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">storage</span><span class="w"> </span><span class="cm">/* as &amp;mut Tester&lt;dyn X&gt; */</span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">storage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Tester</span><span class="w"> </span><span class="cm">/* ::&lt;Bar&gt; */</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="nc">Bar</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* … */</span><span class="w"> </span><span class="p">}};</span><span class="w"></span>
<span class="n">testers</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">storage</span><span class="w"> </span><span class="cm">/* as &amp;mut Tester&lt;dyn X&gt; */</span><span class="p">);</span><span class="w"> </span><span class="c1">// OK.</span>

<span class="n">testers</span><span class="p">.</span><span class="n">test_all</span><span class="p">();</span><span class="w"> </span><span class="c1">// hypothetical API</span>
</code></pre></div>



<a name="232501354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Applications%20of%20trait%20objects%20as%20a%20data%20members/near/232501354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Mazakas <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Applications.20of.20trait.20objects.20as.20a.20data.20members.html#232501354">(Mar 30 2021 at 21:31)</a>:</h4>
<p>Oh, I'm so silly!</p>
<p>I know why I was confused now lol. I didn't realize the <code>as &amp;Tester</code> cast used the default generic value, <code>dyn X</code></p>
<p>Duh!</p>
<p>Yeah, this <a href="https://godbolt.org/z/GPx9dqaP5">compiles fine</a></p>



<a name="232501788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Applications%20of%20trait%20objects%20as%20a%20data%20members/near/232501788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Mazakas <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Applications.20of.20trait.20objects.20as.20a.20data.20members.html#232501788">(Mar 30 2021 at 21:35)</a>:</h4>
<p>Thank you, everyone, for the help!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>